Setup

# UNHCR Demographics
# Author: Connor Kelly
# Date: December 1, 2020

library(tidyverse)
library(plotly)

setwd("C:/Users/Connor/Documents/GitHub/MDI")

# Import Data
agesex <- read_csv("Data/unhcr_popstats_export_demographics_all_data.csv", 
          skip = 3) # Date extracted: 2015-09-18 04:37:44 +02:00 (update?)  
## Warning: 48820 parsing failures.
##  row          col           expected actual                                                   file
## 3230 Female 5-11  1/0/T/F/TRUE/FALSE    88  'Data/unhcr_popstats_export_demographics_all_data.csv'
## 3230 Female 12-17 1/0/T/F/TRUE/FALSE    45  'Data/unhcr_popstats_export_demographics_all_data.csv'
## 3230 Male 5-11    1/0/T/F/TRUE/FALSE    115 'Data/unhcr_popstats_export_demographics_all_data.csv'
## 3230 Male 12-17   1/0/T/F/TRUE/FALSE    38  'Data/unhcr_popstats_export_demographics_all_data.csv'
## 3231 Female 5-11  1/0/T/F/TRUE/FALSE    139 'Data/unhcr_popstats_export_demographics_all_data.csv'
## .... ............ .................. ...... ......................................................
## See problems(...) for more details.
# Examine Data
agesex[is.na(agesex)] <- 0 # Replace missing values as 0
str(agesex) # Both Male and Female 5-11 and 12-17 are logical rather than numeric
## Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 19869 obs. of  19 variables:
##  $ Year                                   : num  2001 2001 2001 2001 2001 ...
##  $ Country / territory of asylum/residence: chr  "Afghanistan" "Afghanistan" "Afghanistan" "Angola" ...
##  $ Location Name                          : chr  "Kabul" "Various" "Herat" "Viana" ...
##  $ Female 0-4                             : num  0 14335 0 484 219 ...
##  $ Female 5-11                            : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Female 5-17                            : num  1 45451 0 1687 734 ...
##  $ Female 12-17                           : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Female 18-59                           : num  1 99880 1 1282 427 ...
##  $ Female 60+                             : num  0 19234 0 43 25 ...
##  $ F: Unknown                             : num  0 412004 0 0 0 ...
##  $ F: Total                               : num  2 590904 1 3496 1405 ...
##  $ Male 0-4                               : num  0 14716 0 597 226 ...
##  $ Male 5-11                              : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Male 5-17                              : num  0 47522 0 1645 711 ...
##  $ Male 12-17                             : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ Male 18-59                             : num  2 114965 1 787 139 ...
##  $ Male 60+                               : num  0 13025 0 34 15 ...
##  $ M: Unknown                             : num  0 435492 0 0 0 ...
##  $ M: Total                               : num  2 625720 1 3063 1091 ...
##  - attr(*, "problems")=Classes 'tbl_df', 'tbl' and 'data.frame': 48820 obs. of  5 variables:
##   ..$ row     : int  3230 3230 3230 3230 3231 3231 3231 3231 3232 3232 ...
##   ..$ col     : chr  "Female 5-11" "Female 12-17" "Male 5-11" "Male 12-17" ...
##   ..$ expected: chr  "1/0/T/F/TRUE/FALSE" "1/0/T/F/TRUE/FALSE" "1/0/T/F/TRUE/FALSE" "1/0/T/F/TRUE/FALSE" ...
##   ..$ actual  : chr  "88" "45" "115" "38" ...
##   ..$ file    : chr  "'Data/unhcr_popstats_export_demographics_all_data.csv'" "'Data/unhcr_popstats_export_demographics_all_data.csv'" "'Data/unhcr_popstats_export_demographics_all_data.csv'" "'Data/unhcr_popstats_export_demographics_all_data.csv'" ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   Year = col_double(),
##   ..   `Country / territory of asylum/residence` = col_character(),
##   ..   `Location Name` = col_character(),
##   ..   `Female 0-4` = col_double(),
##   ..   `Female 5-11` = col_logical(),
##   ..   `Female 5-17` = col_double(),
##   ..   `Female 12-17` = col_logical(),
##   ..   `Female 18-59` = col_double(),
##   ..   `Female 60+` = col_double(),
##   ..   `F: Unknown` = col_double(),
##   ..   `F: Total` = col_double(),
##   ..   `Male 0-4` = col_double(),
##   ..   `Male 5-11` = col_logical(),
##   ..   `Male 5-17` = col_double(),
##   ..   `Male 12-17` = col_logical(),
##   ..   `Male 18-59` = col_double(),
##   ..   `Male 60+` = col_double(),
##   ..   `M: Unknown` = col_double(),
##   ..   `M: Total` = col_double()
##   .. )
agesex = subset(agesex, select=-c(`Female 5-11`, `Female 12-17`, `F: Total`, `Male 5-11`, `Male 12-17`, `M: Total`)) # Drop logical columns and totals

# Aggregate by country of asylum
agesex <- agesex %>%
  group_by(Year, `Country / territory of asylum/residence`) %>%
  summarize_if(is.numeric, sum)

# Establish color palette for plots  
colors <- c( "#FB6A4A", "#EF3B2C", "#CB181D", "#A50F15", "#67000D", # red palette (female)
            "#6BAED6", "#4292C6", "#2171B5", "#08519C", "#08306B") # blue palette (male)

Age and Sex of Displaced in Countries of Asylum

This data shows the age and sex disaggregated breakdown of refugees and asylum seekers residing in the given countries of asylum. It is important to note that this data focuses on the country of residence of refugees and asylum seekers, not their country of origin.

Afghanistan

afg <- agesex %>% filter(`Country / territory of asylum/residence`=="Afghanistan")
afg <- pivot_longer(afg,
                    cols = c(3:12),
                    names_to = "agesex",
                    values_to = "refugees")
afg$agesex <- factor(afg$agesex, levels = c("Female 0-4", "Female 5-17", "Female 18-59", "Female 60+", "F: Unknown",
                                            "Male 0-4", "Male 5-17", "Male 18-59", "Male 60+", "M: Unknown"))

afg_p <- ggplot(afg, aes(x=Year, y=refugees, fill=agesex)) + geom_area() + scale_fill_manual(values = colors) +
  ggtitle("Age and Sex of Displaced in Afghanistan") + ylab("Refugees and Asylum Seekers") + theme(legend.title = element_blank())
ggplotly(afg_p, dynamicTicks = TRUE) %>%
  layout(hovermode = "x")

Bangladesh

bgd <- agesex %>% filter(`Country / territory of asylum/residence`=="Bangladesh")
bgd <- pivot_longer(bgd,
                    cols = c(3:12),
                    names_to = "agesex",
                    values_to = "refugees")
bgd$agesex <- factor(bgd$agesex, levels = c("Female 0-4", "Female 5-17", "Female 18-59", "Female 60+", "F: Unknown",
                                            "Male 0-4", "Male 5-17", "Male 18-59", "Male 60+", "M: Unknown"))

bgd_p <- ggplot(bgd, aes(x=Year, y=refugees, fill=agesex)) + geom_area() + scale_fill_manual(values = colors) +
  ggtitle("Age and Sex of Displaced in Bangladesh") + ylab("Refugees and Asylum Seekers") + theme(legend.title = element_blank())
ggplotly(bgd_p, dynamicTicks = TRUE) %>%
  layout(hovermode = "x")

Burundi

bdi <- agesex %>% filter(`Country / territory of asylum/residence`=="Burundi")
bdi <- pivot_longer(bdi,
                    cols = c(3:12),
                    names_to = "agesex",
                    values_to = "refugees")
bdi$agesex <- factor(bdi$agesex, levels = c("Female 0-4", "Female 5-17", "Female 18-59", "Female 60+", "F: Unknown",
                                            "Male 0-4", "Male 5-17", "Male 18-59", "Male 60+", "M: Unknown"))

bdi_p <- ggplot(bdi, aes(x=Year, y=refugees, fill=agesex)) + geom_area() + scale_fill_manual(values = colors) +
  ggtitle("Age and Sex of Displaced in Burundi") + ylab("Refugees and Asylum Seekers") + theme(legend.title = element_blank())
ggplotly(bdi_p, dynamicTicks = TRUE) %>%
  layout(hovermode = "x")

Central African Republic

caf <- agesex %>% filter(`Country / territory of asylum/residence`=="Central African Rep.")
caf <- pivot_longer(caf,
                    cols = c(3:12),
                    names_to = "agesex",
                    values_to = "refugees")
caf$agesex <- factor(caf$agesex, levels = c("Female 0-4", "Female 5-17", "Female 18-59", "Female 60+", "F: Unknown",
                                            "Male 0-4", "Male 5-17", "Male 18-59", "Male 60+", "M: Unknown"))

caf_p <- ggplot(bdi, aes(x=Year, y=refugees, fill=agesex)) + geom_area() + scale_fill_manual(values = colors) +
  ggtitle("Age and Sex of Displaced in Central African Republic") + ylab("Refugees and Asylum Seekers") + theme(legend.title = element_blank())
ggplotly(caf_p, dynamicTicks = TRUE) %>%
  layout(hovermode = "x")

Chad

tcd <- agesex %>% filter(`Country / territory of asylum/residence`=="Chad")
tcd <- pivot_longer(tcd,
                    cols = c(3:12),
                    names_to = "agesex",
                    values_to = "refugees")
tcd$agesex <- factor(tcd$agesex, levels = c("Female 0-4", "Female 5-17", "Female 18-59", "Female 60+", "F: Unknown",
                                            "Male 0-4", "Male 5-17", "Male 18-59", "Male 60+", "M: Unknown"))

tcd_p <- ggplot(tcd, aes(x=Year, y=refugees, fill=agesex)) + geom_area() + scale_fill_manual(values = colors) +
  ggtitle("Age and Sex of Displaced in Chad") + ylab("Refugees and Asylum Seekers") + theme(legend.title = element_blank())
ggplotly(tcd_p, dynamicTicks = TRUE) %>%
  layout(hovermode = "x")

Ethiopia

eth <- agesex %>% filter(`Country / territory of asylum/residence`=="Ethiopia")
eth <- pivot_longer(eth,
                    cols = c(3:12),
                    names_to = "agesex",
                    values_to = "refugees")
eth$agesex <- factor(eth$agesex, levels = c("Female 0-4", "Female 5-17", "Female 18-59", "Female 60+", "F: Unknown",
                                            "Male 0-4", "Male 5-17", "Male 18-59", "Male 60+", "M: Unknown"))

eth_p <- ggplot(eth, aes(x=Year, y=refugees, fill=agesex)) + geom_area() + scale_fill_manual(values = colors) +
  ggtitle("Age and Sex of Displaced in Ethiopia") + ylab("Refugees and Asylum Seekers") + theme(legend.title = element_blank())
ggplotly(eth_p, dynamicTicks = TRUE) %>%
  layout(hovermode = "x")

Guatemala

gtm <- agesex %>% filter(`Country / territory of asylum/residence`=="Guatemala")
gtm <- pivot_longer(gtm,
                    cols = c(3:12),
                    names_to = "agesex",
                    values_to = "refugees")
gtm$agesex <- factor(gtm$agesex, levels = c("Female 0-4", "Female 5-17", "Female 18-59", "Female 60+", "F: Unknown",
                                            "Male 0-4", "Male 5-17", "Male 18-59", "Male 60+", "M: Unknown"))

gtm_p <- ggplot(gtm, aes(x=Year, y=refugees, fill=agesex)) + geom_area() + scale_fill_manual(values = colors) +
  ggtitle("Age and Sex of Displaced in Guatemala") + ylab("Refugees and Asylum Seekers") + theme(legend.title = element_blank())
ggplotly(gtm_p, dynamicTicks = TRUE) %>%
  layout(hovermode = "x")

Iraq

irq <- agesex %>% filter(`Country / territory of asylum/residence`=="Iraq")
irq <- pivot_longer(irq,
                    cols = c(3:12),
                    names_to = "agesex",
                    values_to = "refugees")
irq$agesex <- factor(irq$agesex, levels = c("Female 0-4", "Female 5-17", "Female 18-59", "Female 60+", "F: Unknown",
                                            "Male 0-4", "Male 5-17", "Male 18-59", "Male 60+", "M: Unknown"))

irq_p <- ggplot(irq, aes(x=Year, y=refugees, fill=agesex)) + geom_area() + scale_fill_manual(values = colors) +
  ggtitle("Age and Sex of Displaced in Iraq") + ylab("Refugees and Asylum Seekers") + theme(legend.title = element_blank())
ggplotly(irq_p, dynamicTicks = TRUE) %>%
  layout(hovermode = "x")

Libya

lby <- agesex %>% filter(`Country / territory of asylum/residence`=="Libya")
lby <- pivot_longer(lby,
                    cols = c(3:12),
                    names_to = "agesex",
                    values_to = "refugees")
lby$agesex <- factor(lby$agesex, levels = c("Female 0-4", "Female 5-17", "Female 18-59", "Female 60+", "F: Unknown",
                                            "Male 0-4", "Male 5-17", "Male 18-59", "Male 60+", "M: Unknown"))

lby_p <- ggplot(lby, aes(x=Year, y=refugees, fill=agesex)) + geom_area() + scale_fill_manual(values = colors) +
  ggtitle("Age and Sex of Displaced in Lybia") + ylab("Refugees and Asylum Seekers") + theme(legend.title = element_blank())
ggplotly(lby_p, dynamicTicks = TRUE) %>%
  layout(hovermode = "x")

Mali

mli <- agesex %>% filter(`Country / territory of asylum/residence`=="Mali")
mli <- pivot_longer(mli,
                    cols = c(3:12),
                    names_to = "agesex",
                    values_to = "refugees")
mli$agesex <- factor(mli$agesex, levels = c("Female 0-4", "Female 5-17", "Female 18-59", "Female 60+", "F: Unknown",
                                            "Male 0-4", "Male 5-17", "Male 18-59", "Male 60+", "M: Unknown"))

mli_p <- ggplot(mli, aes(x=Year, y=refugees, fill=agesex)) + geom_area() + scale_fill_manual(values = colors) +
  ggtitle("Age and Sex of Displaced in Mali") + ylab("Refugees and Asylum Seekers") + theme(legend.title = element_blank())
ggplotly(mli_p, dynamicTicks = TRUE) %>%
  layout(hovermode = "x")

Myanmar

mmr <- agesex %>% filter(`Country / territory of asylum/residence`=="Myanmar")
mmr <- pivot_longer(mmr,
                    cols = c(3:12),
                    names_to = "agesex",
                    values_to = "refugees")
mmr$agesex <- factor(mmr$agesex, levels = c("Female 0-4", "Female 5-17", "Female 18-59", "Female 60+", "F: Unknown",
                                            "Male 0-4", "Male 5-17", "Male 18-59", "Male 60+", "M: Unknown"))

mmr_p <- ggplot(mmr, aes(x=Year, y=refugees, fill=agesex)) + geom_area() + scale_fill_manual(values = colors) +
  ggtitle("Age and Sex of Displaced in Myanmar") + ylab("Refugees and Asylum Seekers") + theme(legend.title = element_blank())
ggplotly(mmr_p, dynamicTicks = TRUE) %>%
  layout(hovermode = "x")

Nigeria

nga <- agesex %>% filter(`Country / territory of asylum/residence`=="Nigeria")
nga <- pivot_longer(nga,
                    cols = c(3:12),
                    names_to = "agesex",
                    values_to = "refugees")
nga$agesex <- factor(nga$agesex, levels = c("Female 0-4", "Female 5-17", "Female 18-59", "Female 60+", "F: Unknown",
                                            "Male 0-4", "Male 5-17", "Male 18-59", "Male 60+", "M: Unknown"))

nga_p <- ggplot(nga, aes(x=Year, y=refugees, fill=agesex)) + geom_area() + scale_fill_manual(values = colors) +
  ggtitle("Age and Sex of Displaced in Nigeria") + ylab("Refugees and Asylum Seekers") + theme(legend.title = element_blank())
ggplotly(nga_p, dynamicTicks = TRUE) %>%
  layout(hovermode = "x")

Syria

syr <- agesex %>% filter(`Country / territory of asylum/residence`=="Syrian Arab Rep.")
syr <- pivot_longer(syr,
                    cols = c(3:12),
                    names_to = "agesex",
                    values_to = "refugees")
syr$agesex <- factor(syr$agesex, levels = c("Female 0-4", "Female 5-17", "Female 18-59", "Female 60+", "F: Unknown",
                                            "Male 0-4", "Male 5-17", "Male 18-59", "Male 60+", "M: Unknown"))

syr_p <- ggplot(syr, aes(x=Year, y=refugees, fill=agesex)) + geom_area() + scale_fill_manual(values = colors) +
  ggtitle("Age and Sex of Displaced in Syria") + ylab("Refugees and Asylum Seekers") + theme(legend.title = element_blank())
ggplotly(syr_p, dynamicTicks = TRUE) %>%
  layout(hovermode = "x")

Sudan

sdn <- agesex %>% filter(`Country / territory of asylum/residence`=="Sudan")
sdn <- pivot_longer(sdn,
                    cols = c(3:12),
                    names_to = "agesex",
                    values_to = "refugees")
sdn$agesex <- factor(sdn$agesex, levels = c("Female 0-4", "Female 5-17", "Female 18-59", "Female 60+", "F: Unknown",
                                            "Male 0-4", "Male 5-17", "Male 18-59", "Male 60+", "M: Unknown"))

sdn_p <- ggplot(sdn, aes(x=Year, y=refugees, fill=agesex)) + geom_area() + scale_fill_manual(values = colors) +
  ggtitle("Age and Sex of Displaced in Sudan") + ylab("Refugees and Asylum Seekers") + theme(legend.title = element_blank())
ggplotly(sdn_p, dynamicTicks = TRUE) %>%
  layout(hovermode = "x")

South Sudan

ssd <- agesex %>% filter(`Country / territory of asylum/residence`=="South Sudan")
ssd <- pivot_longer(ssd,
                    cols = c(3:12),
                    names_to = "agesex",
                    values_to = "refugees")
ssd$agesex <- factor(ssd$agesex, levels = c("Female 0-4", "Female 5-17", "Female 18-59", "Female 60+", "F: Unknown",
                                            "Male 0-4", "Male 5-17", "Male 18-59", "Male 60+", "M: Unknown"))

ssd_p <- ggplot(ssd, aes(x=Year, y=refugees, fill=agesex)) + geom_area() + scale_fill_manual(values = colors) +
  ggtitle("Age and Sex of Displaced in South Sudan") + ylab("Refugees and Asylum Seekers") + theme(legend.title = element_blank())
ggplotly(ssd_p, dynamicTicks = TRUE) %>%
  layout(hovermode = "x")

Somalia

som <- agesex %>% filter(`Country / territory of asylum/residence`=="Somalia")
som <- pivot_longer(som,
                    cols = c(3:12),
                    names_to = "agesex",
                    values_to = "refugees")
som$agesex <- factor(som$agesex, levels = c("Female 0-4", "Female 5-17", "Female 18-59", "Female 60+", "F: Unknown",
                                            "Male 0-4", "Male 5-17", "Male 18-59", "Male 60+", "M: Unknown"))

som_p <- ggplot(som, aes(x=Year, y=refugees, fill=agesex)) + geom_area() + scale_fill_manual(values = colors) +
  ggtitle("Age and Sex of Displaced in Somalia") + ylab("Refugees and Asylum Seekers") + theme(legend.title = element_blank())
ggplotly(som_p, dynamicTicks = TRUE) %>%
  layout(hovermode = "x")

Venezuela

ven <- agesex %>% filter(`Country / territory of asylum/residence`=="Venezuela (Bolivarian Republic of)")
ven <- pivot_longer(ven,
                    cols = c(3:12),
                    names_to = "agesex",
                    values_to = "refugees")
ven$agesex <- factor(ven$agesex, levels = c("Female 0-4", "Female 5-17", "Female 18-59", "Female 60+", "F: Unknown",
                                            "Male 0-4", "Male 5-17", "Male 18-59", "Male 60+", "M: Unknown"))

ven_p <- ggplot(ven, aes(x=Year, y=refugees, fill=agesex)) + geom_area() + scale_fill_manual(values = colors) +
  ggtitle("Age and Sex of Displaced in Venezuela") + ylab("Refugees and Asylum Seekers") + theme(legend.title = element_blank())
ggplotly(ven_p, dynamicTicks = TRUE) %>%
  layout(hovermode = "x")

Yemen

yem <- agesex %>% filter(`Country / territory of asylum/residence`=="Yemen")
yem <- pivot_longer(yem,
                    cols = c(3:12),
                    names_to = "agesex",
                    values_to = "refugees")
yem$agesex <- factor(yem$agesex, levels = c("Female 0-4", "Female 5-17", "Female 18-59", "Female 60+", "F: Unknown",
                                            "Male 0-4", "Male 5-17", "Male 18-59", "Male 60+", "M: Unknown"))

yem_p <- ggplot(yem, aes(x=Year, y=refugees, fill=agesex)) + geom_area() + scale_fill_manual(values = colors) +
  ggtitle("Age and Sex of Displaced in Yemen") + ylab("Refugees and Asylum Seekers") + theme(legend.title = element_blank())
ggplotly(yem_p, dynamicTicks = TRUE) %>%
  layout(hovermode = "x")